package com.wheel.logging.ws.server;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Component;

import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;

/**
 * WebSocket通讯编辑服务
 *
 * @author Administrator
 */
@ServerEndpoint("/ws/log")
@Component
public class WebSocketLogServer {
    @OnMessage
    public void onMessage(String message, Session session) {
        JSONObject event = JSON.parseObject(message);
        String method = event.getString("method");
        if ("init".equalsIgnoreCase(method)) {
            LogSessionManager.getInstance().setSession(session);
        }
    }

    @OnClose
    public void onClose(Session session) {
        LogSessionManager.getInstance().close (session);
    }
}